package cn.cloud.butler.webapi.service;

import cn.cloud.butler.webapi.domain.PermissionEntity;
import cn.cloud.butler.webapi.domain.ProduceNameDistinctResult;
import cn.cloud.butler.webapi.domain.ProducePriceEntity;
import cn.cloud.butler.webapi.logger.WaLogger;
import cn.cloud.butler.webapi.repository.ProducePriceRepository;
import cn.cloud.butler.webapi.repository.ProductSumRepository;
import cn.cloud.butler.webapi.service.cache.ProductPriceServiceCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouguocheng on 2018-12-03-上午 10:38
 */

@Service
@Transactional
public class ProducePriceService {
    private final ProducePriceRepository producePriceRepository;
    private final ProductSumRepository productSumRepository;
    private final PermissonService permissonService;
    private final ProductPriceServiceCache productPriceServiceCache;

    @Autowired
    public ProducePriceService(ProducePriceRepository producePriceRepository,
                               ProductSumRepository productSumRepository,
                               PermissonService permissonService,
                               ProductPriceServiceCache productPriceServiceCache) {
        this.producePriceRepository = producePriceRepository;
        this.productSumRepository = productSumRepository;
        this.permissonService = permissonService;
        this.productPriceServiceCache = productPriceServiceCache;
    }


    @Cacheable(value = "price-key")
    public List<HashMap<String,Object>> selCompProPrice(Long compId) {
        List<HashMap<String,Object>> data = new ArrayList<>();
        List<String> produceSumEntities = productSumRepository.findByCompid(compId);
        for (String produceSumEntity : produceSumEntities) {
            String produceName = produceSumEntity;
            HashMap<String, Object> map = new HashMap<>();
            List<ProducePriceEntity> pList = productPriceServiceCache.getProductPriceEntity(produceName,compId);
            if (pList.size() <= 0) { //如果price为null的话
                map.put("producename", produceName);
                map.put("price", 1);
            } else {
                map.put("producename", produceName);
                map.put("price", pList.get(0).getPrice());
            }
            data.add(map);
        }
        return data;
    }


    //插入价格，如果存在，则进行修改，如果不存在，则更新
    public Map<String, Object> addPrice(String username, String producename, Float price) {
        HashMap<String, Object> map = new HashMap<>();
        PermissionEntity permisson = permissonService.findPermissionByUser(username);
        if (permisson == null) {
           // map.put(1, "查询失败");
            map.put("MsgCode",1);
            map.put("MsgDesc","查询失败");

        } else {
            Long compID = permisson.getCompID();
            List<ProducePriceEntity> pList = producePriceRepository.findByCompIdAndProduceName(compID,producename);
            if (pList.size() <= 0) { //不存在，则进行插入操作
                Integer pid = (new Long(compID).intValue());//将Long转为Integer类型
                try {
                    producePriceRepository.addProductPrice(pid, price, producename);            //保存
                    map.put("MsgCode",0);
                    map.put("MsgDesc","插入成功");
                } catch (Exception e) {
                    map.put("MsgCode",1);
                    map.put("MsgDesc","插入失败"+e.getMessage());
                    WaLogger.logger.error("插入失败！" + e.toString());
                }
            } else {//存在则进行更新操作
                try {
                    producePriceRepository.updateByNameAndCompid(producename, price, compID);
                    map.put("MsgCode",0);
                    map.put("MsgDesc","更新成功");
                } catch (Exception e) {
                    map.put("MsgCode",1);
                    map.put("MsgDesc","更新失败"+e.getMessage());
                    WaLogger.logger.error("更新失败！" + e.toString());
                }
            }

        }
        return map;
    }

    //获得当前厂获取产品名称和价格对应表
    @Cacheable(value="user-key")
    public Map<String, Double> getProducePriceMapByCompId(Long compId){
        Map<String, Double> producePriceMap = new HashMap<>();
        List<ProducePriceEntity> producePriceEntityList = producePriceRepository.findByCompId(compId);
        for (ProducePriceEntity producePriceEntity : producePriceEntityList) {
            producePriceMap.put(producePriceEntity.getProduceName(), producePriceEntity.getPrice());
        }

        return producePriceMap;
    }
}